<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 *  Admin后台账号管理
 */
class AdminUser extends Model
{
    protected $connection = 'guild_admin';
    protected $table = 'admin_users';
    protected $fillable = ['username', 'password', 'name', 'avatar'];

    //角色-代理合作方
    const ROLE_GUILD = 2;

    /**
     * 生成指定权限的账号
     */
    static function createUser($userName = '', $pwd = '', $name = '', $role = self::ROLE_GUILD)
    {
        // \Log::info(__CLASS__ . "==dddddd==");
        // $adminUserModel = config('admin.database.users_model');
        // if (self::where(['username' => $userName])->count()) return false;
        $updateInfo = [
            'username' => $userName,
            'password' => bcrypt($pwd),
            'name' => $name
        ];
        $adminUser = self::updateOrCreate([
            'username' => $userName,
        ], $updateInfo);
        // \Log::info(__CLASS__ . "==adminUser==" . json_encode($updateInfo));

        $info = [
            'user_id' => $adminUser['id'],
            'role_id' => $role
        ];
        AdminUserRole::updateOrCreate($info, $info);
        return $adminUser;
    }
}
